package com.superman.supermarket.client.composite.preorder.info;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.composite.IUIModule;
import com.superman.supermarket.client.composite.preorder.coupon.CouponChooseModule;
import com.superman.supermarket.client.event.PreorderPageEvent;
import com.superman.supermarket.client.event.PreorderPageEvent.PreorderPageEventType;

/**
 * 确认订单 订单信息模块
 * 
 * @author superman
 * @version 2018年8月12日下午10:00:13
 */
public class PreorderOrderInfoModule extends Composite implements IUIModule {

	interface PreorderOrderInfoModuleUiBinder extends UiBinder<Widget, PreorderOrderInfoModule> {
	}

	private static PreorderOrderInfoModuleUiBinder uiBinder = GWT.create(PreorderOrderInfoModuleUiBinder.class);

	private String carryMode;
	@UiField
	protected FlowPanel choosecouponpanel;
	private float countFee, payFee, couponFee = 0;
	@UiField
	protected HTML countfeehtml, postalfeehtml, couponfeehtml, payfeehtml;
	@UiField
	protected HTML countfeenamahtml, postalfeenamehtml, couponnamehtml, couponfeenamehtml, payfeenamehtml;
	private String couponId;
	private boolean hasAvalibleCoupon = false;
	private Map<String, Object> map;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月12日 下午10:00:15
	 */
	public PreorderOrderInfoModule() {
		initWidget(uiBinder.createAndBindUi(this));

		choosecouponpanel.addDomHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (hasAvalibleCoupon) {
					CouponChooseModule chooseModule = new CouponChooseModule();
					chooseModule.setCouponList(XJsonUtil.getList((JSONArray) map.get("COUPON_LIST")), couponId);
					chooseModule.bottom();
				}
			}
		}, ClickEvent.getType());
	}

	/**
	 * 获取快递类型
	 * 
	 * @author superman
	 * @version 2018年8月12日 下午10:03:57
	 * @return carryMode
	 */
	public String getCarryMode() {
		return carryMode;
	}

	@Override
	public Map<String, Object> getData() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 返回应付价格
	 * 
	 * @author superman
	 * @version 2018年8月19日 下午9:33:53
	 * @return
	 */
	public float getPayFee() {
		return payFee;
	}

	/**
	 * 获取总价
	 * 
	 * @author superman
	 * @version 2018年8月12日 下午10:03:46
	 * @return countFee
	 */
	public float getTotalFee() {
		return countFee;
	}

	private void parseChoosedCoupon(List<Map<String, Object>> couponList) {
		boolean hasChoosed = false;
		for (Map<String, Object> map : couponList) {
			hasChoosed = (int) Double.parseDouble(String.valueOf(map.get("STATE"))) == 1;

			if (hasChoosed) {
				String couponName = String.valueOf(map.get("NAME"));
				couponId = String.valueOf(map.get("COUPON_ID"));

				couponnamehtml.setHTML(couponName);
				couponnamehtml.addStyleName("base-color");
				break;
			}
		}
		if (!hasChoosed) {
			couponId = "none";
			couponnamehtml.setHTML("不使用优惠券");
			couponnamehtml.addStyleName("base-color");
		}

		XGinjector.INSTANCE.getEventbusController().fire(new PreorderPageEvent(couponId, PreorderPageEventType.POP_DEFAULT_COUPON));
	}

	@Override
	public void setData(Map<String, Object> map) {

		this.map = map;
		this.countFee = Float.parseFloat(String.valueOf(map.get("COUNT_FEE")));
		this.payFee = Float.parseFloat(String.valueOf(map.get("PAY_FEE")));
		countfeehtml.setHTML("¥&nbsp;" + countFee);

		// 解析快递信息
		if (map.get("POSTAL_FEE") == null)
			postalfeehtml.setHTML("包邮");
		else {
			List<Map<String, Object>> postfeeList = XJsonUtil.getList((JSONArray) map.get("POSTAL_FEE"));
			for (Map<String, Object> map2 : postfeeList) {
				String mode = String.valueOf(map2.get("CARRY_MODE"));
				float fee = Float.parseFloat(String.valueOf(map2.get("CARRY_FEE")));
				int state = (int) Double.parseDouble(String.valueOf(map2.get("STATE")));
				if (state == 1) {

					this.carryMode = mode;

					// 选中状态
					if (mode.equals("EXPRESS")) {
						postalfeehtml.setHTML("快递:&nbsp;¥&nbsp;" + fee);
					} else if (mode.equals("EMS")) {
						postalfeehtml.setHTML("EMS:&nbsp;¥&nbsp;" + fee);
					} else if (mode.equals("SURFACE_MAIL")) {
						postalfeehtml.setHTML("平邮:&nbsp;¥&nbsp;" + fee);
					} else if (mode.equals("SF")) {
						postalfeehtml.setHTML("顺丰:&nbsp;¥&nbsp;" + fee);
					}
				}
			}
		}

		if (map.get("COUPON_FEE") != null) {
			this.couponFee = Float.parseFloat(String.valueOf(map.get("COUPON_FEE")));
		}
		couponfeehtml.setHTML("-¥&nbsp;" + couponFee);

		// 解析优惠券
		if (map.get("COUPON_LIST") == null) {
			hasAvalibleCoupon = false;
			couponnamehtml.setHTML("无可用优惠券");
		} else {

			List<Map<String, Object>> couponList = XJsonUtil.getList((JSONArray) map.get("COUPON_LIST"));
			if (couponList.size() == 0) {
				hasAvalibleCoupon = false;
				couponnamehtml.setHTML("无可用优惠券");
			} else {
				hasAvalibleCoupon = true;
				// 解析已经选中的优惠券
				parseChoosedCoupon(couponList);
			}
		}

		float payFee = Float.parseFloat(String.valueOf(map.get("PAY_FEE")));
		payfeehtml.setHTML("¥&nbsp;" + payFee);

	}

}
